package com.rico.deploy.controller;

import com.github.pagehelper.PageInfo;
import com.rico.common.response.MessageUtils;
import com.rico.common.response.RestResponse;
import com.rico.deploy.entity.Academic;
import com.rico.deploy.entity.College;
import com.rico.deploy.service.AcademicService;
import com.rico.deploy.service.CampusService;
import com.rico.deploy.service.CollegeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 吴泽胜
 * @FileName AcademicController
 * @date 2021-04-23 4:29 下午
 * @Software: IntelliJ IDEA
 */
@Controller
@Slf4j
public class AcademicController {

    @Autowired
    private AcademicService academicService;

    @Autowired
    private CampusService campusService;

    /**
     * 默认获取第一页教学楼列表
     * @return
     */
    @GetMapping("academic/list")
    @ResponseBody
    public RestResponse getlist(Integer page, Integer limit) {
        List<Academic> academics = academicService.selectAll(page, limit);
        PageInfo<Academic> academicsPageInfo = new PageInfo(academics);
        return MessageUtils.success(academicsPageInfo.getList(), (int) academicsPageInfo.getTotal());
    }

    /**
     * 默认获取第一页教学楼列表
     * @return
     */
    @GetMapping("academic/search")
    @ResponseBody
    public RestResponse getparam(String campusName) {
        List<Academic> academics = academicService.selectBycampusName(campusName);
        PageInfo<Academic> academicsPageInfo = new PageInfo(academics);
        return MessageUtils.success(academicsPageInfo.getList(), (int) academicsPageInfo.getTotal());
    }


    @GetMapping("academic/delete")
    @ResponseBody
    public RestResponse deleteById(Integer id) {
        log.info("{}", id);
        String msg = null;
        if ( academicService.deleteAcademicById(id) == 1 )
            msg = "删除成功";
        else
            msg = "删除失败";
        return MessageUtils.success(msg);
    }


    @PostMapping("academic/put")
    @ResponseBody
    public RestResponse put(Academic academic) {

        academic.setCampus_code(campusService.selectByCampusName(academic.getCampus_name()));

        int i = academicService.updateAcademic(academic);
        if ( i == 0 )
            return MessageUtils.fail("编辑失败");

        else
            return MessageUtils.success("编辑成功");
    }

    @PostMapping("academic/post")
    @ResponseBody
    public RestResponse post(Academic academic) {


        academic.setCampus_code(campusService.selectByCampusName(academic.getCampus_name()));
        int i = academicService.insertAcademic(academic);
        if ( i == 0 )
            return MessageUtils.fail("添加失败");

        else
            return MessageUtils.success("添加成功");
    }

    /**
     * 获取html页面
     * @return
     */
    @GetMapping("academicManage")
    public String gethtml(){
        return "page/academicManage";
    }


}
